<template>
  <div>
    <lb-layout
      v-loading="$store.getters.loading"
      element-loading-text="拼命加载中"
      element-loading-spinner="el-icon-loading"
      element-loading-background="#a19f9fb0;"
    >
      <aps-form
        :config="apsForm.formConfig"
        :model="apsForm.formData"
        :formRef="apsForm.formRef"
        :labelWidth="apsForm.labelWidth"
        ref="form"
      ></aps-form>
      <analysis-chart></analysis-chart>
      <lb-table
        :column="tableData.column"
        :data="tableData.data"
        pagination
        background
        layout="total, sizes, prev, pager, next, jumper"
        :page-sizes="[10, 20, 30, 40]"
        :pager-count="5"
        :current-page.sync="currentPage"
        :total="total"
        :page-size="pageSize"
        @size-change="handleSizeChange"
        @p-current-change="handleCurrentChange"
      >
      </lb-table>
    </lb-layout>
  </div>
</template>

<script>
import { list } from "@/api/iot/analysis";
import { formatDate } from "@/utils/index";
import analysisChart from "@/views/statistics/energy/components/analysisChart.vue";
import analysisConst from "@/views/statistics/energy/components/analysis.js";
export default {
  dicts: ["server_type"],
  name: "analysis",
  components: {
    analysisChart,
  },
  data() {
    return {
      apsForm: {
        formRef: "form",
        labelWidth: "120px",
        formData: {},
        formConfig: {
          formAttr: {
            inline: true,
            size: "small",
          },
          properties: {
            //这里是渲染查询表单的表头和类型的数据
            year: {
              typeName: "el-date-picker",
              type: "year",
              "value-format": "yyyy",
              placeholder: "请选择日期",
            },
          },
          formSoltButton: [
            {
              label: "搜索",
              type: "primary",
              typeName: "el-button",
              size: "small",
              icon: "el-icon-search",
              click: this.handlerSearchDetail,
            },
            {
              label: "重置",
              typeName: "el-button",
              size: "small",
              icon: "el-icon-refresh-left",
              click: this.handleReset,
            },
          ],
        },
      },
      tableData: {
        column: [
          {
            prop: "month",
            label: "能耗月份",
            width: "200",
            // showOverflowTooltip: true,
          },
          {
            prop: "name",
            label: "监测点名称",
            showOverflowTooltip: true,
          },
          {
            prop: "code",
            width: "300",
            label: "设备编码",
          },
          {
            prop: "type",
            width: "200",
            label: "设备型号",
          },
          {
            prop: "benqi",
            width: "100",
            label: "本期（kwh）",
          },
          {
            prop: "tongqi",
            width: "200",
            label: "同期(kwh)",
          },
          {
            prop: "tongbi",
            width: "100",
            label: "同比",
          },
          {
            prop: "leiji",
            width: "100",
            label: "累计同比",
          },
        ],
        data: [],
      },
      dataArr: [],
      currentPage: 1,
      pageSize: 10,
      total: 0,
    };
  },
  mounted() {
    this.getBudgetdata();
    this.$refs.form.setFieldValue("year", new Date());
    this.initData();
  },
  methods: {
    getBudgetdata() {
      this.dataArr = {
        gradedList: [
          {
            name: "实际电费/度",
            data: [1, 0.2, 0.5, 0.8, 0.3, 1.5],
            type: "line",
            symbolSize: 10,
            lineStyle: {
              color: "#CE9661",
              width: 2,
              type: "solid",
            },
            itemStyle: {
              borderWidth: 3,
              color: "#CE9661",
              borderColor: "#CE9661",
            },
          },
          {
            name: "尖峰单价",
            data: [0.7, 0.2, 0.8, 0.5, 0.6, 1.2],
            type: "line",
            symbolSize: 10,
            lineStyle: {
              color: "#FF5959",
              width: 2,
              type: "solid",
            },
            itemStyle: {
              borderWidth: 3,
              color: "#FF5959",
              borderColor: "#FF5959",
            },
          },
          {
            name: "高峰单价",
            data: [1.4, 0.3, 0.8, 0.1, 0.5, 1.3],
            type: "line",
            symbolSize: 10,
            lineStyle: {
              color: "#314192",
              width: 2,
              type: "solid",
            },
            itemStyle: {
              borderWidth: 3,
              color: "#314192",
              borderColor: "#314192",
            },
          },
          {
            name: "基础单价",
            data: [1.2, 0.4, 0.7, 0.1, 0.4, 0.9],
            type: "line",
            symbolSize: 10,
            lineStyle: {
              color: "#249AD8",
              width: 2,
              type: "solid",
            },
            itemStyle: {
              borderWidth: 3,
              color: "#249AD8",
              borderColor: "#249AD8",
            },
          },
          {
            name: "低谷单价",
            data: [1, 0.2, 0.5, 0.8, 0.3, 1.5],
            type: "line",
            symbolSize: 10,
            lineStyle: {
              color: "#50A93D",
              width: 2,
              type: "solid",
            },
            itemStyle: {
              borderWidth: 3,
              color: "#50A93D",
              borderColor: "#50A93D",
            },
          },
        ],
        moneyList: [
          {
            name: "实际电费/度",
            data: [1, 0.2, 0.5, 0.8, 0.3, 1.5],
            type: "line",
            symbolSize: 10,
            lineStyle: {
              color: "#CE9661",
              width: 2,
              type: "solid",
            },
            itemStyle: {
              borderWidth: 3,
              color: "#CE9661",
              borderColor: "#CE9661",
            },
          },
          {
            name: "尖峰单价",
            data: [0.7, 0.2, 0.8, 0.5, 0.6, 1.2],
            type: "line",
            symbolSize: 10,
            lineStyle: {
              color: "#FF5959",
              width: 2,
              type: "solid",
            },
            itemStyle: {
              borderWidth: 3,
              color: "#FF5959",
              borderColor: "#FF5959",
            },
          },
          {
            name: "高峰单价",
            data: [1.4, 0.3, 0.8, 0.1, 0.5, 1.3],
            type: "line",
            symbolSize: 10,
            lineStyle: {
              color: "#314192",
              width: 2,
              type: "solid",
            },
            itemStyle: {
              borderWidth: 3,
              color: "#314192",
              borderColor: "#314192",
            },
          },
          {
            name: "基础单价",
            data: [1.2, 0.4, 0.7, 0.1, 0.4, 0.9],
            type: "line",
            symbolSize: 10,
            lineStyle: {
              color: "#249AD8",
              width: 2,
              type: "solid",
            },
            itemStyle: {
              borderWidth: 3,
              color: "#249AD8",
              borderColor: "#249AD8",
            },
          },
          {
            name: "低谷单价",
            data: [1, 0.2, 0.5, 0.8, 0.3, 1.5],
            type: "line",
            symbolSize: 10,
            lineStyle: {
              color: "#50A93D",
              width: 2,
              type: "solid",
            },
            itemStyle: {
              borderWidth: 3,
              color: "#50A93D",
              borderColor: "#50A93D",
            },
          },
        ],
      };
    },
    async initData(currentPage, pageSize, data) {
      this.tableData.data = analysisConst.analysisdata;
      this.total = analysisConst.analysisdata.length;
      // return false;
      // let that = this;
      // let query = {
      //   year: formatDate(new Date(), "yyyy"),
      // };
      // if (currentPage) {
      //   query.pageNum = currentPage;
      // } else {
      //   query.pageNum = that.currentPage;
      // }
      // if (pageSize) {
      //   query.pageSize = pageSize;
      // } else {
      //   query.pageSize = that.pageSize;
      // }
      // if (data) {
      //   query.year =
      //     data && data.year ? formatDate(new Date(data.year), "yyyy") : "";
      // }
      // that.$store.commit("app/updateApiCount", "add");
      // await list(query)
      //   .then((res) => {
      //     that.$store.commit("app/updateApiCount");
      //     if (res.code === 700) {
      //       that.$message.warning(res.msg);
      //       return false;
      //     }
      //     that.tableData.data = res.rows;
      //     that.total = res.total;
      //   })
      //   .catch((err) => {
      //     that.$store.commit("app/updateApiCount");
      //     this.$message.error("系统错误,请联系管理员!10102");
      //   });
    },
    handlerSearchDetail(val) {
      //val===1 删除操作成功后分页边界值处理
      let data = {};
      let currentPage = 1;
      if (val === 1) {
        if (
          this.total === (this.currentPage - 1) * this.pageSize + 1 &&
          this.total !== 0 &&
          this.currentPage > 1
        ) {
          currentPage = this.currentPage - 1;
          this.currentPage = currentPage;
        } else {
          currentPage = this.currentPage;
        }
      } else {
        currentPage = this.currentPage;
      }
      data.year = this.$refs?.form?.getFieldsValue().year;
      this.initData(currentPage, this.pageSize, data);
    },
    handleSizeChange(val) {
      this.currentPage = 1;
      this.pageSize = val;
      this.handlerSearchDetail();
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.handlerSearchDetail();
    },
    handleSizeChange(val) {
      this.currentPage = 1;
      this.pageSize = val;
      this.handlerSearchDetail();
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.handlerSearchDetail();
    },
    handleReset() {
      //重置
      this.$refs?.form?.resetFields();
      this.$refs.form.setFieldValue("year", new Date());
      this.handlerSearchDetail();
    },
  },
};
</script>
